//
//  ModityController.m
//  HappyTravel
//
//  Created by Apple on 2018/8/2.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "ModityController.h"
#import <PGDatePicker/PGDatePickManager.h>
#import "CenterFourView.h"

@interface ModityController () <PGDatePickerDelegate,UITextFieldDelegate>


@property(nonatomic, assign)Boolean  btnScreenLabel;
@property(nonatomic, assign)float  imaApp;

@property (nonatomic, strong) UITextField *dropDownTextField;
@property (nonatomic, strong) CenterFourView *staticsView;

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *monthArr;

@end

@implementation ModityController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = ThemeColor;
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:18],NSForegroundColorAttributeName:COLOR_RGB(255, 255, 255)}];
    
    [self loadData];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

-(UIView *)comapreCallback:(long)selectUblish {
     UIView * barPreview = [[UIView alloc] initWithFrame:CGRectMake(66, 73, 0, 0)];
     UILabel * phoneScreen = [[UILabel alloc] init];
    UIView * uranium = [UIView new];
    uranium.alpha = 0.6;
    uranium.backgroundColor = [UIColor colorWithRed:195 / 255.0 green:10 / 255.0 blue:170 / 255.0 alpha:0.7];
    uranium.frame = CGRectMake(89, 14, 0, 0);
    barPreview.frame = CGRectMake(42, 85, 0, 0);
    barPreview.alpha = 0.1;
    barPreview.backgroundColor = [UIColor colorWithRed:118 / 255.0 green:190 / 255.0 blue:112 / 255.0 alpha:0.9];
    [uranium addSubview:barPreview];
    phoneScreen.frame = CGRectMake(38, 22, 0, 0);
    phoneScreen.alpha = 0.6;
    phoneScreen.backgroundColor = [UIColor colorWithRed:178 / 255.0 green:43 / 255.0 blue:103 / 255.0 alpha:0.1];
    phoneScreen.font = [UIFont systemFontOfSize:16];
    phoneScreen.text = @"";
    phoneScreen.textColor = [UIColor colorWithRed:0 green:0 blue:0 alpha: 0];
    phoneScreen.textAlignment = NSTextAlignmentRight;
    [uranium addSubview:phoneScreen];

    return uranium;

}





- (void)viewDidLoad {

         UIView * approachSatiety = [self comapreCallback:336];

      [self.view addSubview: approachSatiety];



    [super viewDidLoad];
    self.navigationItem.title = @"Statistics";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    [self didUpdateBecome];
}

#pragma mark - CustomMethod
- (void)loadData {
    
    [self.dataArr removeAllObjects];
    self.dataArr = [HTSingleton readModelArrayForDocument:TravelModelPath];
    [self resignBackground:self.dropDownTextField.text];
    [self backView];
    [self hiddenButton];
    [self topView];
}

#pragma mark - 每月平均旅行次数

-(NSString *)listCommon:(long)month {
    NSMutableString *volubility = [NSMutableString new];

    return volubility;

}




- (void)topView{

         NSString * infraction = [self listCommon:6449];

      NSLog(@"%@",infraction);



    CGFloat average = self.monthArr.count/12.0;
    self.staticsView.averageMonth.text = [NSString stringWithFormat:@"%.2f",average];
}

#pragma mark - 获取当年的去过的城市个数和去过的最多的城市

-(Boolean)cityWill:(NSString *)text windowScreen:(Boolean)windowScreen {
     long phonePrefix = 197;
    BOOL iconoclasticCorner = NO;
    phonePrefix -= 30;
    iconoclasticCorner = phonePrefix > 36;

    return iconoclasticCorner;

}




- (void)backView{

         Boolean cellar = [self cityWill:@"video" windowScreen:YES];

      if (!cellar) {
          NSLog(@"ok");
      }



    
    /**
     得到去过的城市个数
     */
    NSMutableArray *allCityArr = [[NSMutableArray alloc]init];
    for (TralvelModel *model in self.monthArr) {
        [allCityArr addObject:model.endLocation];
    }
    NSMutableArray *listAry = [[NSMutableArray alloc]init];
    for (NSString *str in allCityArr) {
        if (![listAry containsObject:str]) {
            [listAry addObject:str];
        }
    }
    self.staticsView.allCity.text = [NSString stringWithFormat:@"%lu",(unsigned long)listAry.count];
    
    
    
    /**
     得到去过最多城市
     */
    NSInteger maxSameObj = 0;
    NSString *cithName;
    
    NSSet *set = [NSSet setWithArray:allCityArr];
    for (NSString *setstring in set) {
        //需要去掉的元素数组
        NSMutableArray *filteredArray = [[NSMutableArray alloc]initWithObjects:setstring, nil];
        NSMutableArray *dataArray = allCityArr;
        NSPredicate * filterPredicate = [NSPredicate predicateWithFormat:@"NOT (SELF IN %@)",filteredArray];
        //过滤数组
        NSArray * reslutFilteredArray = [dataArray filteredArrayUsingPredicate:filterPredicate];
        int number = (int)(dataArray.count-reslutFilteredArray.count);
        
        maxSameObj = maxSameObj > number?maxSameObj:number;
        if (maxSameObj > number) {
            maxSameObj = maxSameObj;
        }else {
            maxSameObj = number;
            cithName = setstring;
        }
    }
    self.staticsView.mostCity.text = cithName;
}

#pragma mark - 获取数据比例显示

-(UIButton *)callbackLogBack:(NSInteger)placeProgress {
    UIButton * frame = [UIButton new];
    [frame setTitle:@"appTravel" forState:UIControlStateNormal];
    [frame setBackgroundImage:[UIImage imageNamed:@"headerImg"] forState:UIControlStateNormal];
    frame.titleLabel.font = [UIFont systemFontOfSize:10];
    [frame setImage:[UIImage imageNamed:@"januaryHeadOctober"] forState:UIControlStateNormal];
    frame.backgroundColor = [UIColor colorWithRed:191 / 255.0 green:69 / 255.0 blue:232 / 255.0 alpha:1.0];
    frame.alpha = 0.7;
    frame.frame = CGRectMake(255, 14, 0, 0);

    return frame;

}




- (void)hiddenButton{

         UIButton * warmly = [self callbackLogBack:8250];

      [self.view addSubview: warmly];



    
    NSMutableArray *heightArr = [[NSMutableArray alloc] init];
    
    for (int i = 1; i < 13; i ++) {
        NSString *currentMonth = [NSString stringWithFormat:@"%d",i];
        NSString *yearAndMonth = [NSString stringWithFormat:@"%@-%@",self.dropDownTextField.text,currentMonth];
        NSInteger allMonth = 0;
        for (TralvelModel *model in self.monthArr) {
            int  comapre = [self setToolBarTintColor:yearAndMonth anotherDate:model.date andFormat:@"yyyy-M" ansRangeInt:6];
            if (comapre == 0){
                allMonth ++;
            }
        }
        
        NSInteger height = 15;
        if (allMonth == 0) {
            allMonth = 1;
        }else {
            allMonth = allMonth + 1;
        }
        
        if (15*allMonth > 171) {
            height = 171;
        }else {
            height = 15*allMonth;
        }
        
        [heightArr addObject:[NSString stringWithFormat:@"%ld",(long)height]];
    }
    
    self.staticsView.januaryHeight.constant = [heightArr[0] integerValue];
    self.staticsView.februaryHeight.constant = [heightArr[1] integerValue];
    self.staticsView.marchHeight.constant = [heightArr[2] integerValue];
    self.staticsView.aprilHeight.constant = [heightArr[3] integerValue];
    self.staticsView.mayHeight.constant = [heightArr[4] integerValue];
    self.staticsView.juneHeight.constant = [heightArr[5] integerValue];
    self.staticsView.julyHeight.constant = [heightArr[6] integerValue];
    self.staticsView.augustHeight.constant = [heightArr[7] integerValue];
    self.staticsView.septemberHeight.constant = [heightArr[8] integerValue];
    self.staticsView.octoberHeight.constant = [heightArr[9] integerValue];
    self.staticsView.novemberHeight.constant = [heightArr[10] integerValue];
    self.staticsView.decemberHeight.constant = [heightArr[11] integerValue];
}


#pragma mark - 获取选中的当年的所有数据
- (void)resignBackground:(NSString *)date{
    
    [self.monthArr removeAllObjects];
    for (TralvelModel *model in self.dataArr) {
        int  comapre = [self coderWayBeen:date anotherDate:model.date andFormat:@"yyyy" ansRangeInt:4];
        if (comapre == 0){
            [self.monthArr addObject:model];
        }
    }
}

-(int )coderWayBeen:(NSString *)date anotherDate:(NSString *)anotherDate andFormat:(NSString *)format ansRangeInt:(NSInteger)range{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSDate *dates = [dateFormatter dateFromString:date];
    anotherDate = [anotherDate substringWithRange:NSMakeRange(0, range)];
    NSDate *another = [dateFormatter dateFromString:anotherDate];
    
    NSComparisonResult result = [dates compare:another];
    if (result == NSOrderedDescending) {
        return 1;
    }else if(result ==NSOrderedSame){
        return 0;
    }else {
        return -1;
    }
}

-(int )setToolBarTintColor:(NSString *)date anotherDate:(NSString *)anotherDate andFormat:(NSString *)format ansRangeInt:(NSInteger)range{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-M-dd HH:mm"];
    NSDate *newAnother = [dateFormatter dateFromString:anotherDate];
    
    NSDateFormatter *dateFormatter11 = [[NSDateFormatter alloc] init];
    [dateFormatter11 setDateFormat:format];
    
    NSDate *dates = [dateFormatter11 dateFromString:date];
    NSString *another = [dateFormatter11 stringFromDate:newAnother];
    NSDate *anotherNewDate = [dateFormatter11 dateFromString:another];
    
    NSComparisonResult result = [dates compare:anotherNewDate];
    if (result == NSOrderedDescending) {
        return 1;
    }else if(result ==NSOrderedSame){
        return 0;
    }else {
        return -1;
    }
}


- (void)didUpdateBecome{
    
    [self.view addSubview:self.dropDownTextField];
    [self.view addSubview:self.staticsView];
    
    [self.staticsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view).offset(113);
    }];
    
    self.staticsView.septemberHeight.constant = 100;
    self.staticsView.mayHeight.constant = 15;
    self.staticsView.januaryHeight.constant = 150;
}

#pragma mark - UITextFieldDelegate

-(Boolean)logLayoutIs:(NSString *)ne tint:(NSArray *)tint {
     CGFloat travelJanuary = 972.0;
     CGFloat geometry = 128.0;
    BOOL declaimImmobilize = NO;
    travelJanuary = travelJanuary;
    travelJanuary = geometry;
    declaimImmobilize = travelJanuary > 11;
    geometry = 6831;
    declaimImmobilize = geometry > 1;

    return declaimImmobilize;

}




- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

         Boolean britainOffset = [self logLayoutIs:@"duplicate" tint:@[@(552), @(63)]];

      if (britainOffset) {
          NSLog(@"aseVc");
      }



    
    PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
    PGDatePicker *datePicker = datePickManager.datePicker;
    datePicker.datePickerMode = PGDatePickerModeYear;
    datePicker.rowHeight = 40;
    datePicker.delegate = self;
    [self presentViewController:datePickManager animated:false completion:nil];
    
    return NO;
}

#pragma mark - PGDatePickerDelegate

-(UIButton *)originHiddenEditing{
     UIImageView * containerSelected = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"impressed"]];
    UIButton * british = [UIButton new];
    british.backgroundColor = [UIColor colorWithRed:165 / 255.0 green:223 / 255.0 blue:178 / 255.0 alpha:0.2];
    british.alpha = 0.8;
    british.frame = CGRectMake(50, 46, 0, 0);
    [british setTitle:@"container" forState:UIControlStateNormal];
    [british setBackgroundImage:[UIImage imageNamed:@"newsViewsOrigin"] forState:UIControlStateNormal];
    british.titleLabel.font = [UIFont systemFontOfSize:14];
    [british setImage:[UIImage imageNamed:@"tatisticsNovember"] forState:UIControlStateNormal];
    containerSelected.frame = CGRectMake(176, 63, 0, 0);
    containerSelected.alpha = 0.5;
    containerSelected.backgroundColor = [UIColor colorWithRed:206 / 255.0 green:249 / 255.0 blue:157 / 255.0 alpha:0.6];
    containerSelected.contentMode = UIViewContentModeScaleAspectFit;
    containerSelected.animationRepeatCount = 8;
    containerSelected.image = [UIImage imageNamed:@"appOotMood"];

    return british;

}




- (void)travleButtonBecome:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {

         UIButton * squelch = [self originHiddenEditing];

      [self.view addSubview: squelch];



    
    self.dropDownTextField.text = [NSString stringWithFormat:@"%ld",(long)dateComponents.year];
    [self loadData];
}

#pragma mark - getter
- (UITextField *)dropDownTextField {
    return _dropDownTextField ?: ({
        _dropDownTextField = [[UITextField alloc]initWithFrame:CGRectMake(ScreenWidth-80-20, 23+SafeAreaTopHeight, 80, 25)];
        _dropDownTextField.text = [HTSingleton getCurrentYear];
        _dropDownTextField.textColor = UIColorOfHex(0x3B3C4A);
        _dropDownTextField.textAlignment = NSTextAlignmentCenter;
        _dropDownTextField.font = [UIFont systemFontOfSize:14];
        _dropDownTextField.delegate = self;
        
        UIButton *dropBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 16)];
        [dropBtn setImage:[UIImage imageNamed:@"tatisticsReeImage"] forState:UIControlStateNormal];
        dropBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _dropDownTextField.rightView = dropBtn;
        _dropDownTextField.rightViewMode = UITextFieldViewModeAlways;
        
        _dropDownTextField.layer.borderColor = UIColorOfHex(0x3B3C4A).CGColor;
        _dropDownTextField.layer.borderWidth = 1.0f;
        _dropDownTextField.layer.cornerRadius = 10;
        _dropDownTextField;
    });
}

- (CenterFourView *)staticsView {
    if (!_staticsView) {
        _staticsView = [[[NSBundle mainBundle] loadNibNamed:@"CenterFourView" owner:self options:nil] lastObject];
    }
    return _staticsView;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (NSMutableArray *)monthArr {
    if (!_monthArr) {
        _monthArr = [[NSMutableArray alloc] init];
    }
    return _monthArr;
}


@end
